package efr.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import efr.dao.AutorDao;
import efr.dao.impl.AutorDaoImpl;
import efr.entity.Autor;

/**
 * Servlet implementation class Op4AutorSubmitServlet
 */
public class Op4AutorSubmitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Op4AutorSubmitServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Op4AutorSubmitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		logger.debug("start doGet()");
		if("Modifica".equals(request.getParameter("action"))){
			String numeAutor = request.getParameter("numeAutor");
			String id = request.getParameter("id");
			Autor autor = new Autor();
			autor.setNumeAutor(numeAutor);
			autor.setId(Integer.valueOf(id));
			AutorDao autorDao = new AutorDaoImpl();
			autorDao.updateAutor(autor);			
		}
		if("Adauga".equals(request.getParameter("action"))){
			String numeAutor = request.getParameter("numeAutor");
			Autor autor = new Autor();
			autor.setNumeAutor(numeAutor);
			AutorDao autorDao = new AutorDaoImpl();
			Integer id = autorDao.addAutor(autor);
		}
		RequestDispatcher view = request.getRequestDispatcher("/ListAutori.do");
		logger.debug("end doGet()");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
